package com.ruoyi.web.controller.tool;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Component
public class AuthenCheckServer {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public boolean check(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (loginUser == null) return false;
        List<String> openIds = loginUser.getOpenId();
        if (openIds == null) return false;
        for (String openId : openIds) {
            Boolean hasKey = stringRedisTemplate.hasKey(openId);
            if (hasKey == null) return false;
            if (!hasKey) return true;
        }
        return false;
    }
}
